'use strict';

/* Controllers */

angular.module('app')
    .controller('AppCtrl',
        ['$scope', '$rootScope', '$translate', '$localStorage', '$window', '$timeout', 'dialogs', '$uibModalStack', '$state', 'DynamicI18n', 'LoginService', 'Constant',
            function ($scope, $rootScope, $translate, $localStorage, $window, $timeout, dialogs, $modalStack, $state, DynamicI18n, LoginService, Constant) {
                var vm = $scope.vm = this;
                // add 'ie' classes to html
                var isIE = !!navigator.userAgent.match(/MSIE/i);
                isIE && angular.element($window.document.body).addClass('ie');
                isSmartDevice($window) && angular.element($window.document.body).addClass('smart');

                // config
                var app = {
                    getMessage: function (code, args) {
                        var result = $translate.instant(code);
                        return ISS.format(result, args);
                    },
                    //校验权限
                    checkPermit: function (code) {
                        return LoginService.checkPermission(code);
                    }
                };
                $scope.app = angular.extend({}, app, window.ISS);
                $scope.CONSTANT = angular.extend({}, Constant);
                {
                    // 国际化相关
                    $scope.i18n = $.extend({}, DynamicI18n);
                }

                function getLoginUser(config) {
                    LoginService.getLoginUser(config).then(function (data) {
                        $scope.app.loginUser = data.loginUser;
                        if ($scope.app.loginUser.imageData === "" || $scope.app.loginUser.imageData === undefined) {
                            $scope.app.loginUser.imageData = "img/a0.jpg";
                        }
                        $scope.app.pwdRule = data.pwdRule;
                    });
                }

                // save settings to local storage
                if (angular.isDefined($localStorage.settings)) {
                    //$scope.app.settings = $localStorage.settings;
                } else {
                    //$localStorage.settings = $scope.app.settings;
                }

                $scope.$watch('app.settings', function () {
                    // save to local storage
                    //$localStorage.settings = $scope.app.settings;
                }, true);

                // angular translate
                if (ISS.isEmpty(ISS.lang)) {
                    ISS.lang = $translate.proposedLanguage();
                }
                $scope.lang = {isopen: false};
                $scope.langs = {en: 'English', zh: 'Chinese'};
                $scope.selectLang = $scope.langs[$translate.proposedLanguage()] || "English";
                $scope.setLang = function (langKey) {
                    var isOpen = !$scope.lang.isopen;

                    // set the current lang
                    $scope.selectLang = $scope.langs[langKey];
                    // You can change the language during runtime
                    $translate.use(langKey);
                    $scope.lang.isopen = isOpen;

                    $.datepicker.setDefaults($.datepicker.regional[langKey]);

                    $scope.i18n.lang = ISS.lang = langKey;
                    $scope.i18n.langDesc = $scope.selectLang = 'lang.' + langKey;

                };

                $scope.setLang(ISS.lang);

                //页面刷新时执行
                if (!$state.includes('login')) {
                    getLoginUser({showDialog: false});
                }
                //登录成功后执行
                $rootScope.$on('login-success', function (event) {
                    getLoginUser();
                });

                window.getMessage = ISS.getMessage = function (code, args) {
                    var result = $translate.instant(code);
                    return ISS.format(result, args);
                }

                function isSmartDevice($window) {
                    var ua = $window['navigator']['userAgent'] || $window['navigator']['vendor'] || $window['opera'];
                    // Checks for iOs, Android, Blackberry, Opera Mini, and Windows mobile devices
                    return (/iPhone|iPod|iPad|Silk|Android|BlackBerry|Opera Mini|IEMobile/).test(ua);
                }

                //设置Dialog的Parent Scope
                dialogs.setScope($scope);

                vm.validateActivateIsEnd = true;
                $scope.$on("$stateChangeStart", function ($event, state, param) {
                    $modalStack.dismissAll("$uibUnscheduledDestruction");
                });
                window.onresize = function () {
                    $rootScope.$broadcast("onWindowResize");
                }

                $scope.$on("ReadMessage", function (event, msg) {
                    $scope.$broadcast("ReadMessageHeader", msg);
                });
                
                $scope.$on("RefreshGrid", function (event, msg) {
                    $scope.$broadcast("RefreshGridFromHeader", msg);
                });
            }]);
